package com.zsc.controller.user;

import com.zsc.context.BaseContext;
import com.zsc.dto.ResetPasswordDTO;
import com.zsc.entity.AddressBook;
import com.zsc.entity.User;
import com.zsc.result.Result;
import com.zsc.service.AddressBookService;
import com.zsc.service.UserService;
import com.zsc.utils.CopyUtil;
import com.zsc.vo.UserInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author 叶文锋
 * @Date 2024/6/21 15:04
 */
@Slf4j
@RestController
@Api(tags = "用户端 个人主页接口")
@RequestMapping("/user/self")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private AddressBookService addressBookService;

    /**
     * 获取用户个人信息
     *
     * @return
     */
    @GetMapping("/info")
    @ApiOperation("获取个人信息")
    public Result<UserInfoVO> selfInfo() {
        log.info("获取用户个人信息");
        Long userId = BaseContext.getCurrentId();
        User user = userService.getById(userId);
        UserInfoVO userInfoVO = CopyUtil.copy(user, UserInfoVO.class);
        return Result.success(userInfoVO);
    }

    /**
     * 重置用户密码
     *
     * @param resetPasswordDTO
     * @return
     */
    @PutMapping("/resetPassword")
    @ApiOperation("重置密码")
    public Result resetPassword(@RequestBody ResetPasswordDTO resetPasswordDTO) {
        log.info("重置密码,密码参数：{}", resetPasswordDTO);
        userService.resetPassword(resetPasswordDTO);
        return Result.success();
    }

    /**
     * 获取当前用户所有地址
     *
     * @return
     */
    @GetMapping("/address/list")
    @ApiOperation("获取当前用户所有地址")
    public Result<List<AddressBook>> list() {
        log.info("获取当前用户所有地址");
        AddressBook addressBook = new AddressBook();
        addressBook.setUserId(BaseContext.getCurrentId());
        List<AddressBook> list = addressBookService.list(addressBook);
        return Result.success(list);
    }

    /**
     * 用户新增地址
     *
     * @param addressBook
     * @return
     */
    @PostMapping("/address")
    @ApiOperation("新增地址")
    public Result saveAddress(@RequestBody AddressBook addressBook) {
        log.info("用户新增地址，地址详情：{}", addressBook);
        addressBookService.save(addressBook);
        return Result.success();
    }

    /**
     * 查询用户默认地址
     *
     * @return
     */
    @GetMapping("/address/default")
    @ApiOperation("查询用户默认地址")
    public Result<AddressBook> getDefault() {
        log.info("查询用户默认地址");
        AddressBook addressBook = new AddressBook();
        addressBook.setIsDefault(1);
        addressBook.setUserId(BaseContext.getCurrentId());
        List<AddressBook> list = addressBookService.list(addressBook);
        if (list != null && list.size() == 1) {
            return Result.success(list.get(0));
        }
        return Result.error("没有查询到默认地址");
    }

    @PutMapping("/address")
    @ApiOperation("根据id修改地址")
    public Result update(@RequestBody AddressBook addressBook) {
        addressBookService.updateById(addressBook);
        return Result.success();
    }

    /**
     * 根据id删除地址
     *
     * @param id
     * @return
     */
    @DeleteMapping("/address")
    @ApiOperation("根据id删除地址")
    public Result deleteById(Long id) {
        addressBookService.removeById(id);
        return Result.success();
    }

    /**
     * 根据id查询地址
     *
     * @param id
     * @return
     */
    @GetMapping("/address/{id}")
    @ApiOperation("根据id查询地址")
    public Result<AddressBook> getById(@PathVariable Long id) {
        AddressBook addressBook = addressBookService.getById(id);
        return Result.success(addressBook);
    }

    @PutMapping("/address/default")
    @ApiOperation("设置默认地址")
    public Result setDefault(@RequestBody AddressBook addressBook) {
        addressBookService.setDefault(addressBook);
        return Result.success();
    }
}
